#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <gtk/gtk.h>
#include <glib/gstdio.h>

#include "invoke_scripts.h"
#include "sig_handle.h"
#include "global_vars.h"

void call_daemon(char *command)
{
	char given_command[COMMAND_LENGTH];
	int dummy;

	FILE *result_pipe;
	int result=-1;

	int sockfd;
	struct sockaddr_un address;
	size_t addr_size;

	GtkWidget *error_dialog;

	result_pipe=popen("pgrep njubras-daemon","r");
	dummy=fread(&result,sizeof(int),1,result_pipe);

	if (result==-1)
		goto cannot_connect_to_server;

	sockfd=socket(AF_UNIX,SOCK_STREAM,0);
	address.sun_family=AF_UNIX;
	strcpy(address.sun_path,SOCKET_FILE);
	addr_size=sizeof(address);

	result=connect(sockfd,(struct sockaddr *)&address,addr_size);

	if (result==-1)
		goto cannot_connect_to_server;

	memset(given_command,'\0',80);
	strcpy(given_command,command);

	dummy=write(sockfd,given_command,COMMAND_LENGTH);

	pclose(result_pipe);
	close(sockfd);

	return;

	cannot_connect_to_server:

	error_dialog=gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,\
																	GTK_BUTTONS_CLOSE,PROMPT_WHEN_FAIL);
	gtk_dialog_run(GTK_DIALOG(error_dialog));
	gtk_widget_destroy(error_dialog);

	g_remove(LOCK_FILE);
	exit(EXIT_FAILURE);
}

void bras_connect()
{
	GString *command;

	GtkWidget *username_entry;
	GtkWidget *password_entry;

	GtkWidget *check_button_off_campus;

	username_entry=g_object_get_data(G_OBJECT(window_global),"username_entry");
	password_entry=g_object_get_data(G_OBJECT(window_global),"password_entry");

	check_button_off_campus=g_object_get_data(G_OBJECT(window_global),"check_button_off_campus");

	g_chdir("./scripts");
	command=g_string_new("bash bras_control.sh ");
	command=g_string_append(command,"start ");
	command=g_string_append(command,gtk_entry_get_text(GTK_ENTRY(username_entry)));
	command=g_string_append(command," ");
	command=g_string_append(command,gtk_entry_get_text(GTK_ENTRY(password_entry)));

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_button_off_campus)))
		command=g_string_append(command," offcampus");

	command=g_string_append(command," &");
	//dummy=system(command->str);
	call_daemon(command->str);

	g_string_free(command,TRUE);
	g_chdir("../");
}

void bras_disconnect()
{
	GString *command;

	GtkWidget *check_button_off_campus;

	unlink(LOCK_FILE);

	g_chdir("./scripts");

	command=g_string_new("bash bras_control.sh ");
	command=g_string_append(command,"stop ");

	check_button_off_campus=g_object_get_data(G_OBJECT(window_global),"check_button_off_campus");

	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_button_off_campus)))
		command=g_string_append(command,"offcampus");

	g_string_append(command," &");

	call_daemon(command->str);

	g_string_free(command,TRUE);
	g_chdir("../");
}

gboolean check_bras_connect()
{
	GtkWidget *check_button_off_campus;

	check_button_off_campus=g_object_get_data(G_OBJECT(window_global),"check_button_off_campus");

	g_chdir("./scripts");

	if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_button_off_campus)))
		call_daemon("bash check_bras_connect.sh &");
	else
		call_daemon("bash check_bras_connect.sh offcampus &");

	g_chdir("../");

	return disconnect_enable;
}


